// services/VideoService.ts

import request from '@/api/request'
import type {
  Video,
  VideoUpload,
  SearchParams,
  VideoListResponse,
  CommentListResponse,
  ImageResponse,
  LikeResponse,
  CommentResponse,
  FavoriteResponse,
} from '@/services/video/video.types'

export default class VideoService {
  // 获取视频列表
  async getVideoList(params: SearchParams): Promise<VideoListResponse> {
    const response: VideoListResponse = await request.get('/vs/works', { params })
    return response
  }

  // 获取视频详情
  async getVideoById(id: string): Promise<Video> {
    const response = await request.get(`/vs/works/${id}`)
    return response.data
  }

  // 上传视频
  async uploadVideo(data: VideoUpload): Promise<string> {
    const formData = new FormData()
    formData.append('title', data.title)
    formData.append('description', data.description)
    formData.append('files', data.videoFile)
    if (data.coverFile) {
      formData.append('files', data.coverFile)
    }

    const response = await request.post('/vs/works', formData, {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    })
    return response.data
  }

  // 点赞视频
  async likeVideo(workId: number): Promise<LikeResponse> {
    const response = await request.post('/vs/like', null, {
      params: { workId },
    })
    return response.data
  }

  // 取消点赞视频
  async unlikeVideo(workId: number): Promise<LikeResponse> {
    const response = await request.delete('/vs/like', {
      params: { workId },
    })
    return response.data
  }

  // 获取视频评论列表
  async getComments(workId: number, pageNum = 1, pageSize = 20): Promise<CommentListResponse> {
    const response = await request.get('/vs/comments', {
      params: { workId, pageNum, pageSize },
    })
    return response.data
  }

  // 发布评论
  async addComment(workId: number, parentId: number, content: string): Promise<CommentResponse> {
    const response = await request.post('/vs/comments', {
      work_id: workId,
      parent_id: parentId,
      content,
    })
    return response.data
  }

  // 删除评论
  async deleteComment(commentId: string): Promise<CommentResponse> {
    const response = await request.delete('/vs/comments', {
      params: { commentId },
    })
    return response.data
  }

  // 获取视频图片列表
  async getVideoImages(workId: string): Promise<ImageResponse> {
    const response: ImageResponse = await request.get(`/vs/works/${workId}/images`)
    return response
  }

  // 获取视频预览图
  async getVideoPreview(workId: string, imageId: string): Promise<Blob> {
    const response = await request.get(`/vs/works/${workId}/images/${imageId}`)
    return response.data
  }

  // 收藏视频
  async favoriteVideo(workId: number): Promise<FavoriteResponse> {
    const response = await request.post('/vs/favorite', null, {
      params: { workId },
    })
    return response.data
  }

  // 取消收藏视频
  async unfavoriteVideo(workId: number): Promise<FavoriteResponse> {
    const response = await request.delete('/vs/favorite', {
      params: { workId },
    })
    return response.data
  }
}
